package dp2.tablet.proyecto.util;

import java.io.InputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
import dp2.tablet.proyecto.Login;
import dp2.tablet.proyecto.abrircaja;

public class JSONCajero extends AsyncTask<String, Void, Void>{
	
	
	InputStream is = null;
	String result = "";
	JSONObject jArray = null;

 
    // constructor
    public JSONCajero() {
 
    }
 
    @Override
	protected Void doInBackground(String... url) {
		// Making HTTP request
    	
    
         HttpParams myParams = new BasicHttpParams();
         HttpConnectionParams.setConnectionTimeout(myParams, 10000);
         HttpConnectionParams.setSoTimeout(myParams, 10000);
         HttpClient httpclient = new DefaultHttpClient( myParams);
//        String json=obj.toString();
         

        try {
      	  
      	  HttpPost httppost = new HttpPost(url[0].toString());
             httppost.setHeader("Content-type", "application/json");

                             
             JSONObject jobject = new JSONObject(); 
             jobject.put("cashier_id", abrircaja.getCaja().getId())	;
             //jobject.put("nuevo_estado",3);
             jobject.put("nuevo_estado",Integer.valueOf(url[1]));
             
          //FileWriter file = new FileWriter("c:\\test.json");
 			//file.write(jobject.toString());
          
          //Toast.makeText(this," -> " + jobject.toString(), Toast.LENGTH_LONG).show();   
                            
         StringEntity se = new StringEntity(jobject.toString());
         se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
 	       httppost.setEntity(se);

 	       HttpResponse response = httpclient.execute(httppost);
 	       String temp = EntityUtils.toString(response.getEntity());
 	       Log.i("tag", temp);
 	    

        }catch (Exception e) {
	    	 Log.e("Buffer Error", "Error converting result " + e.toString());
		}
        // return JSON String
        return null;
	}
	
}
